const router = require('koa-router')()
const db = require('../db/initdb')
const fs = require('fs');
const path = require('path');
const { koaBody } = require('koa-body')

// 素材页面
router.get('/public/material/:name', async (ctx, next) => {
  let filepath = path.join(__dirname, "../public/images/" + ctx.params.name)
  let file = fs.readFileSync(filepath)
  ctx.set('Content-Type', "Access-Control-Allow-Origin:'http://localhost:1234'")
  ctx.body = file
})

router.get('/public/video/:name', async (ctx, next) => {
  // console.log(ctx.params.name);
  let filepath = path.join(__dirname, "../public/images/" + ctx.params.name)
  let file = fs.readFileSync(filepath)
  // ctx.set('Content-Type', "Access-Control-Allow-Origin:'http://localhost:1234'")
  ctx.body = file
})

router.get('/material', async (ctx, next) => {
  let sql = "select hs.card.id,hs.card.name,hs.card.type,hs.card.outline,hs.card.voicenum," +
    "hs.card.perface,hs.user.name username,hs.user.avatar from hs.user, hs.card " +
    "where hs.user.id = hs.card.userid order by hs.card.date desc, hs.card.voicenum desc"
  let result = await db.query(sql);
  // console.log(result.rows);

  ctx.body = JSON.stringify(result.rows);
})

router.get('/material/swiper', async (ctx, next) => {
  let sql = "select hs.card.id,hs.card.name,hs.card.type,hs.card.outline,hs.card.voicenum," +
    "hs.card.perface,hs.user.name username,hs.user.avatar from hs.user, hs.card " +
    "where hs.user.id = hs.card.userid order by hs.card.voicenum desc"
  let result = await db.query(sql);
  // console.log(result.rows);
  let list = [];
  for (let i = 0; i < 3; i++) {
    list.push(result.rows[i]);
  }
  // console.log(list);
  ctx.body = list;
})

// 素材详情页
router.get('/mdetail', async (ctx, next) => {
  let sql = "select hs.card.type, hs.card.name,hs.card.outline,hs.card.voicenum,hs.card.starnum," +
    "hs.card.image,hs.user.name username,hs.user.avatar from hs.user, hs.card " +
    "where hs.card.id=$1 and hs.card.userid=hs.user.id";
  let result = await db.query(sql, [ctx.query.id]);
  // console.log(result.rows);

  ctx.body = JSON.stringify(result.rows);
})

// 用户收藏列表
router.get('/user/star', async (ctx, next) => {
  let sql = "select collect from hs.starcard where " +
    "id=$1 and userid=$2";
  let result = await db.query(sql, [ctx.query.id, ctx.query.userid]);
  // console.log(result.rows[0]);
  ctx.body = result.rows[0]
})

// 点击收藏或取消收藏
router.post('/mdetail/star', async (ctx, next) => {
  // console.log(ctx.request.body);
  let id = ctx.request.body.id;
  let userid = ctx.request.body.userid;
  let sql = "select starnum from hs.card where id=$1";
  let result = await db.query(sql, [id]);
  let num = result.rows[0].starnum;
  let starnum = 0;
  // console.log(result.rows[0]);
  if (ctx.request.body.collect == 1) {
    let sql1 = "delete from hs.starcard where id=$1 and userid=$2";
    let result1 = await db.query(sql1, [id, userid]);
    let sql4 = "update hs.card set starnum=$1 where id=$2 RETURNING starnum"
    let result4 = await db.query(sql4, [num - 1, id]);
    starnum = result4.rows[0].starnum;
  } else {
    let sql2 = "insert into hs.starcard(id,userid,collect,collectdate) values ($1,$2,$3,$4)";
    let result2 = await db.query(sql2, [id, userid, true, Date.now()]);
    let sql3 = "update hs.card set starnum=$1 where id=$2 RETURNING starnum"
    let result3 = await db.query(sql3, [num + 1, id]);
    // console.log(result3.rows[0]);
    starnum = result3.rows[0].starnum;
  }
  ctx.body = { starnum }
})

// 素材收藏列表
router.get('/material/collect', async (ctx, next) => {
  let sql = "select hs.card.id,hs.card.name,hs.card.outline,hs.card.voicenum," +
    "hs.card.perface,hs.user.name username,hs.user.avatar from hs.user, hs.card, hs.starcard " +
    "where hs.starcard.userid = $1 and hs.card.id = hs.starcard.id and hs.user.id = hs.card.userid order by hs.starcard.collectdate desc"
  let result = await db.query(sql, [ctx.query.userid]);
  // console.log(result.rows);

  ctx.body = JSON.stringify(result.rows);
})

// 个人页面资料修改

router.post('/person/intro', async (ctx, next) => {
  let user = ctx.request.body;
  // console.log(user);
  let sql = 'UPDATE hs.user SET intro=$1 where id=$2 RETURNING *';

  let result = await db.query(sql, [user.value, user.userid])
  // console.log(result.rows);
  ctx.body = result.rows[0]
})

router.get('/person', async (ctx, next) => {
  let sql = 'select * from hs.user where id=$1'

  let result = await db.query(sql, [ctx.query.userid])
  ctx.body = result.rows[0]
})
// 修改昵称
router.post('/person/set/name', async (ctx, next) => {
  // console.log(ctx.request.body);
  let user = ctx.request.body;
  let sql = 'UPDATE hs.user SET name=$1 where id=$2 RETURNING *';
  let result = await db.query(sql, [user.name, user.userid])

  ctx.body = result.rows[0];
})

// 修改性别
router.post('/person/set/gender', async (ctx, next) => {
  // console.log(ctx.request.body);
  let user = ctx.request.body;
  let sql = 'UPDATE hs.user SET sex=$1 where id=$2 RETURNING *';
  let result = await db.query(sql, [user.gender, user.userid])

  ctx.body = result.rows[0];
})

// 修改头像
router.post('/person/set/avatar', koaBody(), async (ctx, next) => {
  let userid = ctx.request.query.userid;
  let filename = (ctx.request.files.image.newFilename)
  let sql = 'UPDATE hs.user set avatar=$1 where id=$2 returning *';
  let result = await db.query(sql, [filename, userid]);
  ctx.body = result.rows[0];
});
// 修改背景图
router.post('/person/set/back', koaBody(), async (ctx, next) => {
  let userid = ctx.request.query.userid;
  let filename = (ctx.request.files.image.newFilename)
  let sql = 'UPDATE hs.user set perface=$1 where id=$2 returning *';
  let result = await db.query(sql, [filename, userid]);
  ctx.body = result.rows[0];
});

module.exports = router
